#ifndef __BIT_BAND_H
#define __BIT_BAND_H

// 这里只定义了GPIO ODR和IDR两个寄存器的位带别名区地址，对其他寄存器的没有定义

//SRAM 位带区:      0X2000 0000~0X2010 0000
//SRAM 位带别名区:  0X2200 0000~0X23FF FFFF

//外设位带区:       0X4000 0000~0X4010 0000
//外设位带别名区:   0X4200 0000~0X43FF FFFF

// 把“位带地址+序列号”转换为别名地址的宏
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x02000000+((addr & 0x00FFFFFF)<<5)+(bitnum<<2)) 
//#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x02000000+((addr & 0x00FFFFFF) * 32)+(bitnum * 4)) 
/*
 *addr & 0xF0000000£¬È¡µØÖ·µÄ¸ß4Î»£¬¿´¿´ÊÇ2»¹ÊÇ4£¬ÓÃÓÚÇø·ÖSRAMºÍÍâÉèµØÖ·£¬
 *如果2，+0x0200 0000则=0X2200 0000即是SRAM如果是4，+0x02000000则=0X4200 0000，即是外设
 *
 *addr & 0x000FFFFFF£¬ÆÁ±Îµô¸ßÁ½Î»£¬Ïàµ±ÓÚ-0X2000 0000»òÕß-0X4000 0000£¬½á¹û±íÊ¾Æ«ÒÆÎ»´øÇø¶àÉÙ¸ö×Ö½Ú
 *<<5  µÈÓÚ*8*4£¬ÒòÎªÎ»´øÇøÒ»¸öµØÖ·±íÊ¾Ò»¸ö×Ö½Ú£¬Ò»¸ö×Ö½ÚÓÐ8¸öbit£¬Ò»¸öbit¿ÉÒÔÅòÕÍ³ÉÒ»¸ö×Ö£¬¼´4¸ö×Ö½Ú
 *<<2 µÈÓÚ*4£¬ÒòÎªÒ»¸öÎ»¿ÉÒÔÅòÕÍ³ÉÒ»¸ö×Ö£¬¼´4¸ö×Ö½Ú
 *
 *·Ö½â³ÉÁ½Ìõ¹«Ê½Ó¦¸Ã¾ÍÊÇÕâÑù£º
 *SRAMÎ»´ø±ðÃûµØÖ·
 *AliasAddr= 0x22000000+((A-0x20000000)*8+n)*4 =0x22000000+ (A-0x20000000)*8*4 +n*4
 *ÍâÉèÎ»´ø±ðÃûµØÖ·
 *AliasAddr= 0x22000000+((A-0x20000000)*8+n)*4 =0x22000000+ (A-0x20000000)*8*4 +n*4
 */


// 把地址转换为一个指针
#define MEM_ADDR(addr)  *((volatile unsigned long  *)(addr)) 

// 把位带别名区地址转换成指针
#define BIT_ADDR(addr, bitnum)   MEM_ADDR(BITBAND(addr, bitnum))   

// GPIO ODR ºÍ IDR ¼Ä´æÆ÷µØÖ·Ó³Éä 
#define GPIOA_ODR_Addr    (GPIOA_BASE+12) //0x4001080C   
#define GPIOB_ODR_Addr    (GPIOB_BASE+12) //0x40010C0C   
#define GPIOC_ODR_Addr    (GPIOC_BASE+12) //0x4001100C   
#define GPIOD_ODR_Addr    (GPIOD_BASE+12) //0x4001140C   
#define GPIOE_ODR_Addr    (GPIOE_BASE+12) //0x4001180C   
#define GPIOF_ODR_Addr    (GPIOF_BASE+12) //0x40011A0C      
#define GPIOG_ODR_Addr    (GPIOG_BASE+12) //0x40011E0C      
  
#define GPIOA_IDR_Addr    (GPIOA_BASE+8)  //0x40010808   
#define GPIOB_IDR_Addr    (GPIOB_BASE+8)  //0x40010C08   
#define GPIOC_IDR_Addr    (GPIOC_BASE+8)  //0x40011008   
#define GPIOD_IDR_Addr    (GPIOD_BASE+8)  //0x40011408   
#define GPIOE_IDR_Addr    (GPIOE_BASE+8)  //0x40011808   
#define GPIOF_IDR_Addr    (GPIOF_BASE+8)  //0x40011A08   
#define GPIOG_IDR_Addr    (GPIOG_BASE+8)  //0x40011E08 


// µ¥¶À²Ù×÷ GPIOµÄÄ³Ò»¸öIO¿Ú£¬n(0,1,2...16),n±íÊ¾¾ßÌåÊÇÄÄÒ»¸öIO¿Ú
#define PAout(n)   BIT_ADDR(GPIOA_ODR_Addr,n)  //Êä³ö   
#define PAin(n)    BIT_ADDR(GPIOA_IDR_Addr,n)  //ÊäÈë   
  
#define PBout(n)   BIT_ADDR(GPIOB_ODR_Addr,n)  //Êä³ö   
#define PBin(n)    BIT_ADDR(GPIOB_IDR_Addr,n)  //ÊäÈë   
  
#define PCout(n)   BIT_ADDR(GPIOC_ODR_Addr,n)  //Êä³ö   
#define PCin(n)    BIT_ADDR(GPIOC_IDR_Addr,n)  //ÊäÈë   
  
#define PDout(n)   BIT_ADDR(GPIOD_ODR_Addr,n)  //Êä³ö   
#define PDin(n)    BIT_ADDR(GPIOD_IDR_Addr,n)  //ÊäÈë   
  
#define PEout(n)   BIT_ADDR(GPIOE_ODR_Addr,n)  //Êä³ö   
#define PEin(n)    BIT_ADDR(GPIOE_IDR_Addr,n)  //ÊäÈë  
  
#define PFout(n)   BIT_ADDR(GPIOF_ODR_Addr,n)  //Êä³ö   
#define PFin(n)    BIT_ADDR(GPIOF_IDR_Addr,n)  //ÊäÈë  
  
#define PGout(n)   BIT_ADDR(GPIOG_ODR_Addr,n)  //Êä³ö   
#define PGin(n)    BIT_ADDR(GPIOG_IDR_Addr,n)  //ÊäÈë  


#endif
